﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Lidgren.Network;
using Lidgren.Network.Xna;
using Microsoft.Xna.Framework;

namespace D2D2.SharedInterfaces.Networking
{
    public class ChestObject : BaseMessage
    {
        private float _timeSinceHit;
        private float _targetVolume;

        public ChestObject()
        {

        }

        public ChestObject(NetIncomingMessage message):base(message)
        {

        }

        public override GameMessageType MessageType
        {
            get { return GameMessageType.ChestState; }
        }

        public float TotalVolume { get; set; }
        public float CurrentVolume { get; set; }
        public int ActionPerHit { get; set; }
        public int VolumePerHit { get; set; }
        public Vector2 Location { get; set; }
        public bool Alive { get; set; }
        public float HitTime { get; set; }
        public bool InDecreasingHit { get; set; }

        public Rectangle Rectangle
        {
            get { return new Rectangle((int)Location.X, (int)Location.Y, GetWidth(), GetHeight()); }
        }

        public PlayerObject TargetPlayer { get; set; }

        public void Hit(PlayerObject player)
        {
            _timeSinceHit = 0;
            InDecreasingHit = true;
            _targetVolume = CurrentVolume - VolumePerHit;
        }

        public bool CalculateLive(float elapsed)
        {
            if (InDecreasingHit)
            {
                _timeSinceHit += elapsed;
                float remainingTime = HitTime - (_timeSinceHit - elapsed);

                float deltaVolume = (CurrentVolume - _targetVolume)*elapsed/remainingTime;

                float calculatedVolume = CurrentVolume - deltaVolume;

                if (calculatedVolume <= _targetVolume)
                {
                    CurrentVolume = _targetVolume;

                    InDecreasingHit = false;
                    _timeSinceHit = 0;
                }
                else
                    CurrentVolume = MathHelper.Max(_targetVolume, calculatedVolume);

                if (calculatedVolume <= 0)
                    Alive = false;

            }
            return true;
        }

        public override void Encode(NetOutgoingMessage message)
        {
            message.Write(Id);
            message.Write(TotalVolume);
            message.Write(CurrentVolume);
            message.Write(ActionPerHit);
            message.Write(VolumePerHit);
            message.Write(Location);
            message.Write(Alive);
        }

        public override void Decode(NetIncomingMessage message)
        {
            Id = message.ReadInt64();
            TotalVolume = message.ReadFloat();
            CurrentVolume = message.ReadFloat();
            ActionPerHit = message.ReadInt32();
            VolumePerHit = message.ReadInt32();
            Location = message.ReadVector2();
            Alive = message.ReadBoolean();
        }

        public ChestObject Clone()
        {
            ChestObject chest = new ChestObject
                                    {
                                        ActionPerHit = ActionPerHit,
                                        CurrentVolume = CurrentVolume,
                                        Id = Id,
                                        LocalTime = LocalTime,
                                        Location = Location,
                                        MessageTime = MessageTime,
                                        TargetPlayer = TargetPlayer,
                                        TotalVolume = TotalVolume,
                                        VolumePerHit = VolumePerHit
                                    };

            return chest;

        }

        public int GetWidth()
        {
            return 80;
        }

        public int GetHeight()
        {
            return 60;
        }
    }
}
